package com.mcx.modules.data;

import com.mcx.modules.common.MMO_DataStream;
import com.mcx.modules.common.SecurityUtil;
import com.mcx.modules.common.ZLibUtils;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.HashMap;

public class GameDataTableParser {
    public GameDataTableParser(String path){
        m_FieldNameDic = new HashMap<String, Integer>();

        //------------------
        //第1步：读取数据流
        //------------------
        byte[] buffer = LocalFileMgr.GetBuffer(path);

        //------------------
        //第2步：解压缩
        //------------------
        buffer = ZLibUtils.decompress(buffer);

        //------------------
        //第3步：xor解密
        //------------------
        buffer = SecurityUtil.Xor(buffer);

        //------------------
        //第4步：解析数据到数组
        //------------------
        MMO_DataStream ms = new MMO_DataStream();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            ms.inputStream = inputStream;
            m_Row = ms.ReadInt();
            m_Column = ms.ReadInt();

            m_GameData = new String[m_Row][m_Column];
            m_FieldName = new String[m_Column];

            for (int i = 0; i < m_Row; i++)
            {
                for (int j = 0; j < m_Column; j++)
                {
                    String str = ms.ReadUTF8String();

                    if (i == 0)
                    {
                        //表示读取的是字段
                        m_FieldName[j] = str;
                        m_FieldNameDic.put(str, j);

                    }
                    else if (i > 2)
                    {
                        //表示读取的是内容
                        m_GameData[i][j] = str;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /// <summary>
    /// 行数
    /// </summary>
    private int m_Row;

    /// <summary>
    /// 列数
    /// </summary>
    private int m_Column;

    /// <summary>
    /// 字段名称
    /// </summary>
    private String[] m_FieldName;

    /// <summary>
    /// 字段名称
    /// </summary>
    public String[] FieldName()
    {
        return m_FieldName;
    }

    /// <summary>
    /// 游戏数据
    /// </summary>
    private String[][] m_GameData;

    /// <summary>
    /// 当前行号
    /// </summary>
    private int m_CurRowNo = 3;

    /// <summary>
    /// 字段名称字典
    /// </summary>
    private HashMap<String, Integer> m_FieldNameDic;

    /// <summary>
    /// 是否结束
    /// </summary>
    public Boolean Eof()
    {
        return m_CurRowNo == m_Row;
    }

    public void Next()
    {
        if (Eof()) return;
        m_CurRowNo++;
    }

    public String GetFieldValue(String fieldName)
    {
        try
        {
            if (m_CurRowNo < 3 || m_CurRowNo >= m_Row) return null;
            return m_GameData[m_CurRowNo][m_FieldNameDic.get(fieldName)];
        }
        catch (Exception e) { return null; }
    }

    public Integer GetFieldValueToInt(String fieldName)
    {
        try
        {
            if (m_CurRowNo < 3 || m_CurRowNo >= m_Row) return null;
            return Integer.parseInt(m_GameData[m_CurRowNo][m_FieldNameDic.get(fieldName)]);
        }
        catch (Exception e) { return null; }
    }

    public Float GetFieldValueToFloat(String fieldName)
    {
        try
        {
            if (m_CurRowNo < 3 || m_CurRowNo >= m_Row) return null;
            return Float.parseFloat(m_GameData[m_CurRowNo][m_FieldNameDic.get(fieldName)]);
        }
        catch (Exception e) { return null; }
    }

    public String GetFieldValueToUTF8String(String fieldName)
    {
        return GetFieldValue(fieldName);
    }
}
